export class ArrayUtils {
  /**
   * 获取数组最后一项
   * @param array
   * @returns
   */
  static last<T>(array: Array<T>): T | null {
    if (Array.isArray(array) && array.length > 0) {
      return array[array.length - 1]
    }
    return null
  }

  /**
   * 通过count进行分组(数组升维)
   * @param array
   * @param count
   * @returns
   */
  static groupByCount<T>(array: Array<T>, count): Array<Array<T>> {
    if (!Array.isArray(array)) {
      throw new TypeError('groupByCount 输入类型错误')
    }
    if (typeof count !== 'number' || count <= 0) {
      throw new TypeError('count 需为正整数')
    }
    let index = 0
    const result = []
    while (index < array.length) {
      result.push(array.slice(index, (index += count)))
    }
    return result
  }
}
